<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-operator">
        <a-button v-action:add type="primary" icon="plus" @click="handleAdd">新建</a-button>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :showPagination="false"
        :columns="columns"
        :data="loadData"
      >
        <span slot="status" slot-scope="text, record">
          <template v-if="record.status">
            <a-badge :status="record.status === '1' ? 'success' : 'default'" :text="record.status === '1' ? '审核通过' : '审核拒绝'"/>
          </template>
          <template v-else>
            <span>-</span>
          </template>
        </span>
        <span slot="avatar" slot-scope="text, record">
          <a-avatar shape="square" :src="$staticPrefix(record.user.avatar)" />
        </span>
        <span slot="content_text" slot-scope="text, record">
          <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
        </span>
        <span slot="created_at" slot-scope="text">
          <span>{{ text | dayjs }}</span>
        </span>
        <span slot="updated_at" slot-scope="text">
          <span>{{ text | dayjs }}</span>
        </span>

        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record, 1)" v-action:query>查看</a>
            <template v-if="!record.status">
              <a-divider type="vertical" v-action:edit/>
              <a @click="handleEdit(record, 2)" v-action:edit>修改</a>
              <a-divider type="vertical" v-action:delete/>
              <a @click="handleEdit(record, 3)" v-action:delete>删除</a>
              <a-divider type="vertical" v-action:check/>
            <a @click="handleEdit(record, 4, 1)" v-action:check>审核通过</a>
              <a-divider type="vertical" v-action:check/>
            <a @click="handleEdit(record, 4, 2)" v-action:check>审核拒绝</a>
            </template>
          </template>
        </span>
      </s-table>

      <create-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleCancel"
        @ok="handleOk"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import {STable, Ellipsis} from '@/components'
import {getList, add, edit, deletes} from '@/api/publicity_manage'
import cloneDeep from 'lodash'
import CreateForm from './modules/CreateForm'

const columns = [
  {
    title: '头像',
    dataIndex: 'avatar',
    scopedSlots: {customRender: 'avatar'}
  },
  {
    title: '申请人',
    align: 'center',
    dataIndex: 'user',
    customRender: (e, record) => {
      return record.user?.nickName
    }
  },
  {
    title: '宣传标题',
    align: 'center',
    dataIndex: 'title',
    scopedSlots: {customRender: 'title'}
  },
  {
    title: '宣传内容',
    align: 'center',
    dataIndex: 'content_text',
    scopedSlots: {customRender: 'content_text'}
  },
  {
    title: '审核时间',
    align: 'center',
    dataIndex: 'check_time',
    customRender: e => {
      return e ? moment(e).format('YYYY-MM-DD HH:mm:ss') : '-'
    }
  },
  {
    title: '审核状态',
    align: 'center',
    dataIndex: 'status',
    scopedSlots: {customRender: 'status'}
  },
  {
    title: '创建时间',
    align: 'center',
    dataIndex: 'created_at',
    scopedSlots: {customRender: 'created_at'}
  },
  {
    title: '更新时间',
    align: 'center',
    dataIndex: 'updated_at',
    scopedSlots: {customRender: 'updated_at'}
  },
  {
    title: '操作',
    align: 'center',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: {customRender: 'action'}
  }
]

export default {
  name: 'publicityManage',
  components: {
    STable,
    Ellipsis,
    CreateForm,
  },
  data() {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getList(requestParameters)
          .then(res => {
            return res.data
          })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  methods: {
    handleAdd() {
      this.mdl = {
        disabled: false
      }
      this.visible = true
    },
    handleEdit(record, index, status) {
      if (index === 1) {
        this.visible = true
        record.disabled = true;
        this.mdl = {...record}
      } else if (index === 2) {
        this.visible = true
        record.disabled = false;
        this.mdl = {...record}
      } else if (index === 3){
        this.confirmLoading = true
        deletes(record.id).then(ret => {
          this.visible = false
          this.confirmLoading = false
          // 刷新表格
          this.$refs.table.refresh()
          this.$message.info('删除成功')
        })
      } else {
        this.confirmLoading = true
        edit({
          status: status.toString()
        },record.id).then(ret => {
          this.visible = false
          this.confirmLoading = false
          // 刷新表格
          this.$refs.table.refresh()
          this.$message.info('审核成功')
        })
      }
    },
    handleOk(e) {
      const form = this.$refs.createModal
      this.confirmLoading = true
      const values = {
        ...e,
      }
      values.user_id = this.$store.state.user.info.id?.toString();
      if (values.id) {
        edit(values, values.id).then(res => {
          this.visible = false
          this.confirmLoading = false
          // 重置表单数据
          form.resetFields()
          if (res.success) {
            // 刷新表格
            this.$refs.table.refresh()
            this.$message.info('修改成功')
          } else {
            this.$message.info(ret.msg)
          }
        })
      } else {
        add(values).then(res => {
          this.visible = false
          this.confirmLoading = false
          // 重置表单数据
          form.resetFields()
          if (res.success) {
            // 刷新表格
            this.$refs.table.refresh()
            this.$message.info('新增成功')
          } else {
            this.$message.info(res.msg)
          }
        })
      }
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal
      form.resetFields() // 清理表单数据（可不做）
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
